home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Dev / misc / LOCCounter.lha / LOCCounter / src / Tally.cpp < prev    next >
C/C++ Source or Header  |  2004-08-18  |  2KB  |  120 lines

  1. /****************************************************************************
  2. *
  3. * $RCSfile: Tally.cpp $
  4. * $Revision: 2.14 $
  5. * $Date: 2004/08/17 23:02:55 $
  6. * $Author: ssolie $
  7. *
  8. *****************************************************************************
  9. *
  10. * Copyright (c) 2004 Steven Solie.  All Rights Reserved.
  11. *
  12. *****************************************************************************
  13. *
  14. * Tally.cpp -- Tally component
  15. *
  16. * The Tally class is used to tally the results of lines of code counting
  17. * and the print the results in a report.
  18. */
  19. #include "Tally.h"
  20.  
  21. #include "FileCount.h"
  22. #include "FileTally.h"
  23.  
  24. #include <proto/dos.h>
  25.  
  26. #include <algorithm>
  27. #include <boost/bind.hpp>
  28.  
  29.  
  30. namespace {
  31.  
  32. typedef boost::shared_ptr<FileTally> FileTallyPtr;
  33.  
  34.  
  35. class Comparator {
  36. public:
  37.     bool operator ()(const FileTallyPtr& lhs, const FileTallyPtr& rhs) const;
  38. };
  39.  
  40. bool Comparator::operator ()(const FileTallyPtr& lhs,
  41.     const FileTallyPtr& rhs) const
  42. {
  43.     return *lhs < *rhs;
  44. }
  45.  
  46. };
  47.  
  48.  
  49. Tally::Tally(bool diff_mode) :
  50.     m_num_files(0),
  51.     m_total_loc(0),
  52.     m_total_added_loc(0),
  53.     m_total_deleted_loc(0),
  54.     m_diff_mode(diff_mode)
  55. {
  56. }
  57.  
  58.  
  59. Tally::~Tally()
  60. {
  61. }
  62.  
  63.  
  64. void Tally::addFile(FileCount& fc)
  65. {
  66.     if ( m_diff_mode )  {
  67.         FileTallyPtr file(new FileTally(fc.getName(), fc.getAddedCount(),
  68.             fc.getDeletedCount()));
  69.         m_files.push_back(file);
  70.  
  71.         m_total_added_loc += fc.getAddedCount();
  72.         m_total_deleted_loc += fc.getDeletedCount();
  73.     }
  74.     else  {
  75.         FileTallyPtr file(new FileTally(fc.getName(), fc.getCount()));
  76.         m_files.push_back(file);
  77.  
  78.         m_total_loc += fc.getCount();
  79.     }
  80.  
  81.     ++m_num_files;
  82. }
  83.  
  84.  
  85. void Tally::print(const BPTR fh)
  86. {
  87.     m_files.sort(Comparator());
  88.  
  89.     std::for_each(m_files.begin(), m_files.end(),
  90.         boost::bind(&FileTally::print, _1, fh));
  91.  
  92.     if ( m_num_files > 1 )  {
  93.         IDOS->FPrintf(fh, "\n");
  94.         printTotal(fh);
  95.     }
  96. }
  97.  
  98.  
  99. void Tally::printTotal(const BPTR fh) const
  100. {
  101.     if ( m_diff_mode )  {
  102.         IDOS->FPrintf(fh, "%8lu Total added LOC\n", m_total_added_loc);
  103.         IDOS->FPrintf(fh, "%8lu Total deleted LOC\n", m_total_deleted_loc);
  104.     }
  105.     else  {
  106.         IDOS->FPrintf(fh, "%8lu Total LOC\n", m_total_loc);
  107.     }
  108. }
  109.  
  110.  
  111. void Tally::printQuick(const BPTR fh) const
  112. {
  113.     if ( m_diff_mode )  {
  114.         IDOS->FPrintf(fh, "0");
  115.     }
  116.     else  {
  117.         IDOS->FPrintf(fh, "%lu", m_total_loc);
  118.     }
  119. }
  120.